<?php
/**
 * Copyright (c) 2016.  hangzhou BOYE .Co.Ltd. All rights reserved
 */

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-12-17
 * Time: 15:00
 */
namespace app\admin\controller;

use app\src\admin\helper\AdminSessionHelper;
use app\src\category\logic\CategoryLogic;
use app\src\file\logic\UserPictureLogic;
use app\src\freight\logic\FreightTemplateLogic;
use app\src\goods\action\ProductAddAction;
use app\src\goods\action\ProductDetailAction;
use app\src\goods\action\ProductEditAction;
use app\src\goods\logic\ProductFaqLogic;
use app\src\goods\logic\ProductGroupLogic;
use app\src\goods\logic\ProductLogic;
use app\src\goods\logic\ProductPropLogic;
use app\src\goods\logic\ProductSkuLogic;
use app\src\goods\logic\SkuLogic;
use app\src\goods\logic\SkuvalueLogic;
use app\src\store\logic\StoreLogic;
use app\src\system\logic\DatatreeLogicV2;
use app\src\system\logic\ProvinceLogic;
use think\Request;

class Product extends Admin
{










    /**
     * 商品详情页/新增
     * @param $get .productid  商品ID
     * @param $get .store_id 店铺ID
     */
    public function detail()
    {
        if (IS_GET) {
            //$productid = $this->_param('id','');
            $id = $this->_param('id', 0);
            $store_id = $this->_param('store_id', 0);
            if (empty($id)) {
                $this->error("缺少商品ID");
            }
            if (empty($store_id)) {
                $this->error("缺少店铺ID");
            }
            $map['id'] = $id;


            $result = (new ProductLogic())->getInfo($map);

            if ($result['status']) {
                $detail = $result['info']['detail'];
            } else {
                $this->error("商品信息获取失败！");
            }

            $this->assign("detail", $detail);
            $this->assign("id", $id);
            $this->assign("store_id", $store_id);
            return $this->boye_display();

        } else {
            $detail = $this->_param("detail", '');
            $id = $this->_param("id", '');

            $map['id'] = $id;

            $result =  (new ProductLogic())->save($map, array('detail' => $detail));
            if ($result['status']) {
                $this->success("修改成功！");
            } else {
                $this->error($result['info']);
            }
        }
    }

    /**
     * 首页/商品管理页面
     */
    public function index()
    {

        $name = $this->_param('name', '');
        $onshelf = $this->_param('onshelf', 1);
        $map = array();
        if (!empty($name)) {
            $map['name'] = array('like', '%' . $name . '%');
            $params['name'] = $name;
        }


        $map['onshelf'] = $onshelf;
        $map['status'] = 1;

        $page = array('curpage' => $this->_param('p', 0), 'size' => config('LIST_ROWS'));
        $order = " create_time desc ";
        $params = array('onshelf' => $onshelf);
        $result = (new ProductLogic())->manager($map, $page, $order,$params);

        if ($result['status']) {

            $this->assign('onshelf', $onshelf);
            $this->assign('name', $name);
            $this->assign('show', $result['info']['show']);
            $this->assign('list', $result['info']['list']);


            return $this->boye_display('default','Product/index');
        } else {
            $this->error($result['info']);
        }



    }



    /**
     * 上架的商品
     */
    public function shelfOn(){
        $onshelf = 1;
        return $this->manager($onshelf);
    }

    /**
     * 下架的商品
     */
    public function shelfOff(){
        $onshelf = 0;
        return $this->manager($onshelf);
    }

    /**
     * 商品上下架
     * @internal param 删除成功后跳转 $success_url
     */
    public function shelf()
    {
        $status = $this->_param('on/d', 0);
        $id = $this->_param('id', -1);
        $map = array('id' => $id);


        $entity['onshelf'] = $status;
        $result = (new ProductLogic())->save($map, $entity);

        if ($result['status'] === false) {
            $this->error($result['info']);
        } else {
            $this->success(L('RESULT_SUCCESS'));
        }

    }



    /**
     * 批量上下架
     */

    /**
     * 单个删除
     * @param 删除成功后跳转|bool $success_url 删除成功后跳转
     */
    public function delete()
    {

        //TODO: 检测商品的其它数据是否存在
        $id = $this->_param('id', -1);
        $map = array(
            'id' => $id,
        );

        $result = (new ProductLogic())->pretendDelete($map);

        if ($result['status'] === false) {
            $this->error($result['info']);
        } else {
            $this->success(L('RESULT_SUCCESS'));
        }

    }

    /**
     * 批量删除
     */
    public function bulkDelete()
    {
        $ids = $this->_param('ids/a', []);
        if (empty($ids)) $this->error('参数缺失');
        $map = ['id' => ['in', $ids]];
        $result = (new ProductLogic())->pretendDelete($map);

        if ($result['status'] === false) {
            $this->error($result['info']);
        } else {
            $this->success(L('RESULT_SUCCESS'));
        }

    }

    /**
     * 商品预创建－选择类目




    /**
     * 商品信息编辑
     */
    public function edit()
    {

        if (IS_GET) {
            $onshelf = $this->_param('onshelf', 0);

            $id = $this->_param('id', 0);


            $result = (new ProductDetailAction())->detail($id);

            if ($result['status']) {


                $this->assign("vo", $result['info']);
            }


//dump($result);
            $this->assign('onshelf', $onshelf);

            return $this->boye_display();
        } else {
            $onshelf = $this->_param('onshelf', 0);
            $id = $this->_param('id', 0);
            $params = Request::instance()->param();
            $params['uid']=UID;
//dump($params);exit;
            $result = (new ProductEditAction())->edit($params);
            if(!$result['status']) $this->error($result['info']);
            $this->assign('onshelf', $onshelf);
            $this->success("操作成功!", url('Admin/Product/index'));
        }
    }



    public function add()
    {

        if (IS_GET) {

            return $this->boye_display();
        } else {
            $onshelf = $this->_param('onshelf', 0);
            $main_img = $this->_param('main_img', 0);
            if(empty($main_img)){
                $this->error("主图缺失",'');
            }

            $id = $this->_param('id', 0);
            $params = Request::instance()->param();
            $params['uid']=UID;
//dump($params);exit;
            $result = (new ProductEditAction())->add($params);
            if(!$result['status']) $this->error($result['info']);
            $this->assign('onshelf', $onshelf);
            $this->success("操作成功!", url('Admin/Product/index'));
        }
    }


    //==========================私有方法
//



}